package com.example.xquicdemo

import android.R.attr.data
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.inuker.bluetooth.library.BluetoothClient
import com.inuker.bluetooth.library.beacon.Beacon
import com.inuker.bluetooth.library.search.SearchRequest
import com.inuker.bluetooth.library.search.SearchResult
import com.inuker.bluetooth.library.search.response.SearchResponse
import com.inuker.bluetooth.library.utils.BluetoothLog
import lecho.lib.hellocharts.model.*
import lecho.lib.hellocharts.util.ChartUtils
import lecho.lib.hellocharts.view.ColumnChartView
import lecho.lib.hellocharts.view.LineChartView
import java.lang.String


//https://github.com/dingjikerbo/Android-BluetoothKit
open class BleActivity : AppCompatActivity() {
    var listQuic_1= mutableListOf<PointValue>()//quic算法一
    var listQuic_2= mutableListOf<PointValue>()// quic算法二
    var lisTtcp= mutableListOf<PointValue>() //普通tcp
    lateinit var context:Context
    override fun onCreate(savedInstanceState: Bundle?) {
        this.context=this
        super.onCreate(savedInstanceState)
        /**
         * 连接成功
         */
//        mClient.connect(
//            MAC
//        ) { code, profile ->
//            if (code == REQUEST_SUCCESS) {
//            }
//        }
//        mClient.read(
//            MAC, serviceUUID, characterUUID
//        ) { code, data ->
//            if (code == REQUEST_SUCCESS) {
//            }
//        }
         //写入数据，最大20b
//        mClient.write(
//            MAC, serviceUUID, characterUUID, bytes
//        ) { code ->
//            if (code == REQUEST_SUCCESS) {
//            }
//        }
    }
    //蓝牙写入数据
    fun writeBle(callBack:(()->Unit)){
        //是否开启蓝牙测试
        if (SetCache.getBle(this)){
            //开启ble读取数据
            AppStart.instance().write{
                callBack.invoke()
            }
        }else{
            callBack.invoke()
        }

//           mClient.write(
//            MAC, serviceUUID, characterUUID, bytes
//        ) { code ->
//            if (code == REQUEST_SUCCESS) {
//            }
//        }
    }
    /**
     * 绘制图表
     * 直通图总时间对比
     * 折线图每个算法对比
     *
     *
     */
    fun loadCharShow(){
        ////////////////////////////////////
        //listQuic_1.add(PointValue(1f,2f))
        //listQuic_2.add(PointValue(3f,4f))
        //lisTtcp.add(PointValue(5f,6f))
         //直方图
        generateDefaultData( listQuic_1.sumOf {
            it.y.toInt()
        }, listQuic_2.sumOf {
            it.y.toInt()
        }, lisTtcp.sumOf {
            it.y.toInt()
        })
        val lines = mutableListOf<Line>()
        lines.add(Line(listQuic_1).setColor(Color.RED).setCubic(false).setHasLabelsOnlyForSelected(true))
        lines.add(Line(listQuic_2).setColor(Color.BLUE).setCubic(false).setHasLabelsOnlyForSelected(true))
        lines.add(Line(lisTtcp).setColor(Color.YELLOW).setCubic(false).setHasLabelsOnlyForSelected(true))

        findViewById<LineChartView>(R.id.data_char).lineChartData=LineChartData().apply {
            setLines(lines)
            axisXBottom= Axis().apply {
                setHasTiltedLabels(true)
                textColor = Color.GRAY
                name="顺序"
                var list= mutableListOf<AxisValue>()
                /**
                 * 获取x轴数据
                 */
                for(i in 0..19){
                    list.add(AxisValue(i.toFloat()).setLabel(i.toString()+""))
                }
                values = list
            }
            axisYLeft=Axis().apply {
                setHasTiltedLabels(true)
                textColor = Color.GRAY
                name = "耗时（ms）"
//                var list= mutableListOf<AxisValue>()
//                /**
//                 * 获取y轴数据
//                 */
//                for(i in 0..19){
//                    list.add(AxisValue(i.toFloat()).setLabel(i.toString()+"ms"))
//                }
//                values = list
            }
        }

    }

    /**
     * 图表
     */
    private fun generateDefaultData(q1AllTime:Int,q2AllTime:Int,httpAllTime:Int) {
        val chart=findViewById<ColumnChartView>(R.id.alldate_char)
        val numColumns = arrayListOf(q1AllTime,q2AllTime,httpAllTime)
        val colorChart = arrayListOf(Color.RED,Color.BLUE,Color.YELLOW)
        // Column can have many subcolumns, here by default I use 1 subcolumn in each of 8 columns.
        val columns: MutableList<Column> = ArrayList()
        var values: MutableList<SubcolumnValue?>
        numColumns.forEachIndexed { index, it ->
            values = ArrayList()
            values.add(
                SubcolumnValue(
                    it.toFloat(),
                    colorChart.get(index)
                    // ChartUtils.pickColor()
                )
            )
            val column = Column(values)
            columns.add(column)
        }
        var data = ColumnChartData(columns)
        val axisX = Axis()
        //axisX.setTextColor(Color.BLUE)
        var listX= mutableListOf<AxisValue>()
        listX.add(AxisValue(0f).setLabel("quic(一)"))
        listX.add(AxisValue(1f).setLabel("quic(二)"))
        listX.add(AxisValue(2f).setLabel("http"))
        axisX.setValues(listX)
        val axisY = Axis().setHasLines(true)
        axisX.name = "类别"
        axisY.name = "总耗时（ms）"
        data.setAxisXBottom(axisX)
        data.setAxisYLeft(axisY)
        chart.setColumnChartData(data)
    }
}